package com.spzx.product.controller;

import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-08-11
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    /**
     * 分页查询商品SPU列表
     *
     * @return
     */
    @Operation(summary = "分页查询商品SPU列表")
    @GetMapping("/list")
    public TableDataInfo list() {
        //1. 分页查询
        startPage();
        //2. 调用业务层查询商品列表
        List<Product> list = productService.selectProductList();
        //3. 封装分页对象
        return getDataTable(list);
    }


    /**
     * 保存商品（SPU、SKU列表、SPU详情、库存）
     *
     * @param product
     * @return
     */
    @Operation(summary = "保存商品（SPU、SKU列表、SPU详情、库存）")
    @PostMapping
    public AjaxResult saveProduct(@RequestBody Product product) {
        productService.saveProduct(product);
        return AjaxResult.success();
    }

    /**
     * 修改商品上架或下架
     * @param spuId
     * @param status
     * @return
     */
    @Operation(summary = "商品上架或下架")
    @GetMapping("/updateStatus/{spuId}/{status}")
    public AjaxResult updateStatus(@PathVariable("spuId") Long spuId,
                                   @PathVariable("status") Integer status) {
        if (status != 1 && status != -1) {
            throw new ServiceException("状态值有误");
        }
        productService.updateStatus(spuId,status);
        return AjaxResult.success();
    }

    /**
     * 删除商品
     * @param ids
     * @return
     */
    @Operation(summary = "删除商品")
    @DeleteMapping("{ids}")
    public AjaxResult remove(@Parameter(description = "商品id列表",example = "[1,2]") @PathVariable List<Long> ids){
        productService.removeProduct(ids);
        return AjaxResult.success();
    }
}
